package com.csu.letaobackend.controller;


import com.alibaba.fastjson.JSONObject;
import com.csu.letaobackend.bean.Admin;
import com.csu.letaobackend.bean.Seller;
import com.csu.letaobackend.bean.User;
import com.csu.letaobackend.commom.CommonResponse;
import com.csu.letaobackend.service.AdminService;
import com.csu.letaobackend.service.SellerService;
import com.csu.letaobackend.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.util.DigestUtils;//MD5加密

import javax.servlet.http.HttpSession;

@Controller
@CrossOrigin
@RequestMapping("/account")
public class AccountController {

      @Autowired
      private UserService userService;

      @Autowired
      private SellerService sellerService;

      @Autowired
      private AdminService adminService;

      @PostMapping("/users/login")
      @ResponseBody
      public CommonResponse<User> login(@RequestParam String username, @RequestParam String password, HttpSession session) {

//            password = DigestUtils.md5DigestAsHex(password.getBytes());//加密
            CommonResponse<User> response = userService.getUser(username, password);
            if (response.isSuccess()) {
                  session.setAttribute("login_user", response.getData());
            }
            return response;
      }


      @PostMapping("/users/logout")
      @ResponseBody
      public CommonResponse<String> logout(HttpSession session) {
            User login_user = (User) session.getAttribute("login_user");
            if (login_user != null) {
                  session.removeAttribute("login_user");
                  return CommonResponse.createForSuccessMessage("退出登录成功");
            }
            return CommonResponse.createForError("用户未登录，不能退出");
      }

      @PostMapping("/users/register")
      @ResponseBody
      public CommonResponse<User> register(User user, HttpSession session) {

            CommonResponse<User> response = userService.registerUser(user);
            if (response.isSuccess()) {
                  session.setAttribute("login_user", response.getData());
            }
            return response;
      }

      @PostMapping("/users/loginByCode")
      @ResponseBody
      public CommonResponse<User> loginByCode(@RequestParam String phone, @RequestParam String code, HttpSession session) {

            JSONObject code1 = (JSONObject) session.getAttribute("code");
            CommonResponse response = CommonResponse.createForError("验证码错误或已过期");

            if (code1 != null && code1.getString("code").equals(code) && code1.getString("memPhone").equals(phone)) {
                  response = userService.getUserByPhone(phone);
                  if (response.isSuccess()) {
                        session.setAttribute("login_user", response.getData());
                  }
            }
            return response;
      }


      @GetMapping("/users/info")
      @ResponseBody
      public CommonResponse<User> getUserinfo(HttpSession session) {

            User login_user = (User) session.getAttribute("login_user");

            return userService.getUser(login_user.getUsername());
      }

      @PutMapping("/users")
      @ResponseBody
      public CommonResponse<User> updateUser(User user, HttpSession session) {

            return userService.updateUser(user);
      }

      @PutMapping("/users/password")
      @ResponseBody
      CommonResponse<User> updateUserPassword(@RequestParam String username, @RequestParam String password, HttpSession session) {
            return userService.updateUserPassword(username, password);
      }

      @PutMapping("/users/passwordbycode")
      @ResponseBody
      CommonResponse<User> updateUserPassword(@RequestParam String phone,
                                              @RequestParam String password,
                                              @RequestParam String code,HttpSession session) {
            JSONObject code1 = (JSONObject) session.getAttribute("code");
            if (code1 != null && code1.getString("code").equals(code) && code1.getString("memPhone").equals(phone)) {
                  if (userService.getUserByPhone(phone).getStatus() != 0)
                        return CommonResponse.createForError("该手机号对应账户不存在");
                  User user = userService.getUserByPhone(phone).getData();
                  return userService.updateUserPassword(user.getUsername(), password);
            } else
                  return CommonResponse.createForError("验证码错误或已过期");
            }

      @PutMapping("/sellers/passwordbycode")
      @ResponseBody
      CommonResponse<Seller> updateSellerPassword(@RequestParam String phone,
                                              @RequestParam String password,
                                              @RequestParam String code, HttpSession session) {
            JSONObject code1 = (JSONObject) session.getAttribute("code");
            if (code1 != null && code1.getString("code").equals(code) && code1.getString("memPhone").equals(phone)) {
                  if(sellerService.getSellerByPhone(phone).getStatus()!=0)
                        return CommonResponse.createForError("该手机号对应账户不存在");
                  Seller seller = sellerService.getSellerByPhone(phone).getData();
                  return sellerService.updateSellerPassword(seller.getUsername(),password);
            } else
                  return CommonResponse.createForError("验证码错误或已过期");
      }

      @GetMapping("/users/check")
      @ResponseBody
      CommonResponse checkUsername(@RequestParam String username, HttpSession session) {
            if (userService.getUser(username).getStatus() != 0)
                  return CommonResponse.createForSuccess("用户名可用");
            else
                  return CommonResponse.createForError("用户名已经存在");
      }


      @PostMapping("/sellers/login")
      @ResponseBody
      public CommonResponse<Seller> loginSeller(@RequestParam String username, @RequestParam String password, HttpSession session) {
            CommonResponse<Seller> response = sellerService.getSeller(username, password);
            if (response.isSuccess()) {
                  session.setAttribute("login_seller", response.getData());
            }
            return response;
      }


      @PostMapping("/sellers/logout")
      @ResponseBody
      public CommonResponse<String> logoutSeller(HttpSession session) {
            Seller login_seller = (Seller) session.getAttribute("login_seller");
            if (login_seller != null) {
                  session.removeAttribute("login_seller");
                  return CommonResponse.createForSuccessMessage("退出登录成功");
            }
            return CommonResponse.createForError("用户未登录，不能退出");
      }

      @PostMapping("/sellers/register")
      @ResponseBody
      public CommonResponse<Seller> registerSeller(@RequestParam Seller seller, HttpSession session) {

            CommonResponse<Seller> response = sellerService.registerSeller(seller);
            if (response.isSuccess()) {
                  session.setAttribute("login_seller", response.getData());
            }
            return response;
      }

      @PostMapping("/sellers/loginByCode")
      @ResponseBody
      public CommonResponse<Seller> loginByCode2(@RequestParam String phone, @RequestParam String code, HttpSession session) {

            JSONObject code1 = (JSONObject) session.getAttribute("code");
            CommonResponse response = CommonResponse.createForError("验证码错误或已过期");

            if (code1 != null && code1.getString("code").equals(code) && code1.getString("memPhone").equals(phone)) {
                  response = sellerService.getSellerByPhone(phone);
                  if (response.isSuccess()) {
                        session.setAttribute("login_seller", response.getData());
                  }
            }
            return response;
      }
}
